package com.gxa.customer.security.config;


import com.gxa.customer.security.filter.TokenAuthFilter;
import com.gxa.customer.security.filter.TokenLoginFilter;
import com.gxa.customer.security.security.handler.DefaultPasswordEncoder;
import com.gxa.customer.security.security.handler.MyAccessDeniedHandler;
import com.gxa.customer.security.security.handler.MyLogoutHandler;
import com.gxa.customer.security.security.handler.UnauthEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {





    @Autowired
    private RedisTemplate redisTemplate;//redis

    @Autowired
    private UserDetailsService userDetailsService;//查询用户信息和权限信息
    @Autowired
    private MyAccessDeniedHandler myAccessDeniedHandler;//登陆了403无权限访问

    @Autowired
    private DefaultPasswordEncoder defaultPasswordEncoder;//密码加密


    /**
     * 配置设置
     * @param http
     * @throws Exception
     */
    //设置退出的地址和token，redis操作地址
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new UnauthEntryPoint())//没有登陆的无权限访问
                .accessDeniedHandler(myAccessDeniedHandler)//登陆了403无权限访问
                .and().csrf().disable()
                .authorizeRequests()
                .antMatchers("/customer/test").hasAnyAuthority("system:menu:index")
                .antMatchers("/customer/test3").hasAnyAuthority("system:menu:index2")
                .antMatchers("/admin/acl/login").permitAll()//放行登陆接口,这路径是security默认路径
                .anyRequest().authenticated()
                .and().logout().logoutUrl("/admin/acl/logout")//退出路径,这路径是security默认路径
                .addLogoutHandler(new MyLogoutHandler(redisTemplate)).and()
                .addFilter(new TokenLoginFilter(authenticationManager(), redisTemplate))//认证
                .addFilter(new TokenAuthFilter(authenticationManager(),  redisTemplate)).httpBasic();//授权

    }

    //调用userDetailsService和密码处理
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
            .passwordEncoder(defaultPasswordEncoder);
    }


    //配置不进行认证的路径，可以直接访问，配置放行的静态资源
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**","/css/**");
    }
}
